package com.soft.controller;

import com.soft.pojo.User;
import com.soft.service.UserService;
import com.soft.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruoxi
 * @create 2021-12-24 11:46
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String inputCode = req.getParameter("code");
        String code = (String) req.getSession().getAttribute("RANDOMCODE_IN_SESSION");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        UserService userService = new UserService();

        Result result = userService.login(user,inputCode,code);
        if (result.getSuccess()) {
            req.getSession().setAttribute("successname", username);
            resp.sendRedirect(req.getContextPath()+"/main.jsp");
        } else {
            req.setAttribute("loginFileMsg", result.getMsg());
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }

    }
}
